package main

import "fmt"

func isValid(s string) bool {
	var stack []rune

	for _, v := range s {
		//左半边入栈
		if v == '(' || v == '[' || v == '{' {
			stack = append(stack, v)
		}

		//右半边出栈
		if v == ')' || v == ']' || v == '}' {
			l := len(stack)
			if l == 0 {
				return false
			}

			p := stack[l-1]

			if (v == ')' && p == '(') || (v == ']' && p == '[') || (v == '{' && p == '}') {
				stack = stack[:l-1]
			} else {
				return false
			}
		}
	}

	if len(stack) == 0 {
		return true
	} else {
		return false
	}
}

func main() {
	fmt.Println(isValid("]"))
}
